Fix crash when HttpResponseDecoder receives non-JSON response#1405
Fix crash when HttpResponseDecoder receives non-JSON response#1405ThomsonTan merged 4 commits intomainfrom
Conversation
lib/http/HttpResponseDecoder.cpp
Outdated
| std::string body(response.GetBody().begin(), response.GetBody().end()); | ||
|
|
||
| // Validate that the body is valid JSON before attempting to parse | ||
| if (body.empty() || !nlohmann::json::accept(body)) |
There was a problem hiding this comment.
The wrapper try/catch seems no longer needed with this validation?
There was a problem hiding this comment.
Thanks! With the validation approach using parse() with allow_exceptions=false, the try/catch wrapper is no longer needed. I've removed it in the latest commit.
The function now handles invalid JSON gracefully through return value checking rather than exception handling, making the code cleaner and more efficient.
Changes pushed in commit ab8790c.
Replace double-parsing approach (accept() then parse()) with single parse() call using allow_exceptions=false and is_discarded() check. This eliminates redundant parsing and removes the need for try/catch. Addresses review comments from lalitb and ThomsonTan. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add validation before JSON parsing in processBody to prevent crashes when receiving non-JSON HTTP responses (e.g., HTML from captive portals). Uses nlohmann::json::accept() to validate response body before parsing. Fixes #1338 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Replace double-parsing approach (accept() then parse()) with single parse() call using allow_exceptions=false and is_discarded() check. This eliminates redundant parsing and removes the need for try/catch. Addresses review comments from lalitb and ThomsonTan. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
ab8790c to
ca99f1c
Compare
Pass body iterators directly to nlohmann::json::parse() instead of copying into std::string first. This improves performance by avoiding unnecessary memory allocation and copy. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Remove the leftover scope braces from the try/catch removal and fix indentation for cleaner code structure. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
@ThomsonTan, @lalitb - thank you both for the reviews. Could I get some help merging this one? I see that the CIs for iPhone/Mac are failing (but similar failures are seen across previously merged PRs too) |
I'll do the merge, thanks for the fix. @hanselip |
Summary
This PR fixes issue #1338 where the application crashes when
HttpResponseDecoder::processBodyreceives a non-JSON HTTP response (e.g., HTML from captive portals or network sign-in pages).Changes
processBodymethodnlohmann::json::accept()to validate response body before attempting to parseProblem
When a network request returns a non-JSON response (such as HTML from a captive portal), the library attempts to parse it as JSON, causing a crash in the
nlohmann::json::parse()function.Solution
The fix adds a check using
nlohmann::json::accept()which safely validates whether the response body is valid JSON before attempting to parse it. If validation fails, the function logs an error and returns early.Testing
nlohmann::json::accept()method which is designed for safe validationFixes #1338